<template>
  <div>
    <!-- //面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary">添加用户</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="rolesList" border stripe>
        <!-- 表格第一列 -->
        <el-table-column type="expand" label="#">
          <!-- 扩张列表 -->
          <template v-slot="scoped">
            <!-- 一级权限 -->
            <el-row
              :class="{bt:inx == 0,bb:true}"
              v-for="(item,inx) in scoped.row.children"
              :key="item.id"
            >
              <el-col :span="5" class="Center">
                <el-tag>{{item.authName}}</el-tag>
                <i class="el-icon-s-promotion"></i>
              </el-col>

              <!-- 二级权限 -->
              <el-col :span="19">
                <el-row v-for="(item1,inx1) in item.children" :key="inx1" :class="{bt:inx1 > 0}">
                  <el-col :span="6">
                    <el-tag @close="delRolse" closable type="success">
                      {{item1.authName}}
                      <i class="el-icon-s-promotion"></i>
                    </el-tag>
                  </el-col>
                  <!-- 三级权限 -->
                  <el-col :span="18">
                    <el-tag
                      closable
                      type="warning"
                      v-for="(item2,inx2) in item1.children"
                      :key="inx2"
                      @close="delRolse(scoped.row.id,item2.id)"
                    >
                      {{item2.authName}}
                      <i class="el-icon-s-promotion"></i>
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <!-- 表格第二列 -->
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>

        <!-- 第三列 -->
        <el-table-column label="操作">
          <!-- v-slot ele中的作用域插槽 -->
          <template>
            <el-button type="primary" icon="el-icon-edit" circle>编辑</el-button>
            <el-button size="mini" type="warning" plain>删除</el-button>
            <el-button size="mini" @click="treeFn" type="danger">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!--分配权限弹出框模板 -->
    <el-dialog title="提示" :visible.sync="treeIsShow" width="50%" >
      <!-- 树形控件 树在这-->
       <el-tree
        show-checkbox default-expand-all 
        node-key='id'
        :data="treeDataList"
        :props="defaultProps"
        default-checked-keys
        ></el-tree>

      <span slot="footer" class="dialog-footer">
        <el-button @click="treeIsShow = false">取 消</el-button>
        <el-button type="primary" @click="treeIsShow = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getUserRolesAPI } from '@/api'
//导入移除指定角色权限API
import { delRolseRightAPI } from '@/api'
//导入分配用户角色权限树API
import { getUserRightsTreeAPI } from '@/api'
export default {
  data() {
    return {
      rolesList: [],
      //分配树是否显示
      treeIsShow:false,
      //分配用户权限树数据
      treeDataList:[],
       defaultProps: {
          children: 'children',
          label: 'authName'
      }
    }
  },
  methods: {
    //获取角色列表方法
    async getRolesList() {
      const { data } = await getUserRolesAPI()
      if (data.meta.status != 200) return this.msg.error(data.meta.msg)
      this.rolesList = data.data
    },
    delFn() {},
    //移除管理员相应权限
    async delRolse(a, b) {
      //弹出警示框  tan是自定义弹出警示框
      const res = await this.tan('此操作将永久删除该权限, 是否继续?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).catch(err => err)
      if (res != 'confirm') return this.msg.error('取消移除成功!')
      const res1 = await delRolseRightAPI(a,b)
      console.log(res1);
    },
    //重新分配用户管理员权限树函数
    async treeFn(){
      const {data} = await getUserRightsTreeAPI()
      if(data.meta.status != 200) return this.msg.error(data.meta.msg)
      this.treeDataList = data.data
      this.treeIsShow = true
      console.log( this.treeDataList);
    }
  },
  created() {
    this.getRolesList()
  }
}
</script>

<style scoped>
.bt {
  border-top: 1px solid #cccccc;
}

.bb {
  border-bottom: 1px solid #ccc;
}

.el-tag {
  margin: 5px;
}

.Center {
  display: flex;
  align-items: center;
}
</style>